package com.rxmvp.basemvp;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.loper7.base.ui.BaseActivity;
import com.loper7.base.utils.AppManager;
import com.loper7.base.widget.dialog.SweetAlertDialog;
import com.rxmvp.http.exception.ApiException;
import com.rxmvp.http.exception.ErrorType;

public abstract class BaseMvpActivity<V, T extends BasePresenter<V>> extends BaseActivity {

    public static boolean LOGIN_PUSH_OUT = false;


    public T presenter;
    public V view;

    public int page = 1;//请求页数
    public boolean isFirst = true;//是否第一次网络请求

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        presenter = initPresenter();
        presenter.attach((V) this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onDestroy() {
        presenter.dettach();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
    public abstract T initPresenter();

    public void requestException(ApiException e) {
        switch (e.code) {
            case ErrorType.HTTP_ERROR://网络错误
                showWarningMessage(e.message);
                break;
            case ErrorType.NETWORD_ERROR://协议出错
                showWarningMessage(e.message);
                break;
            case ErrorType.OPERATE_ERROR://接口操作失败
                showErrorMessage(e.message);
                break;
            case ErrorType.PARSE_ERROR://解析出错
                showErrorMessage(e.message);
                break;
            case ErrorType.LOGIN_OUT://设备顶登 或者  未登录
                try {
                    // TODO: 2018/12/11 update login activity 
                    Intent intent = new Intent(AppManager.getAppManager().currentActivity(), Class.forName("com.dw.crude_merchants.ui.setting.login.LoginActivity"));
                    LOGIN_PUSH_OUT = true;
                    AppManager.getAppManager().currentActivity().startActivity(intent);
                    AppManager.getAppManager().finishActivity();
                } catch (ClassNotFoundException e1) {
                    e1.printStackTrace();
                }
                break;
            case ErrorType.UN_BIND_PHONE:
                break;
            case ErrorType.UNKNOWN://未知错误（拦截到的其他错误:如json解析错误）
                if (e.message.length() < 28) {
                    showErrorMessage(e.getMessage());
                }
                break;
        }
    }

}
